﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;

namespace d1Project.Tools.JsonParse
{
    public class doJsonNode
    {
        public doJsonNode()
        {

            this.CaseSensitive = true;
            this.dictValues = new Dictionary<string, doJsonValue>();
            this.listValues = new List<doJsonValue>();
            this.listKeys = new List<string>();
        }

        public void LoadDataFromStream(JsonTextReader _reader)
        {
            while (_reader != null && _reader.Read())
            {
                string _childName = null;
                switch (_reader.TokenType)
                {
                    case JsonToken.PropertyName:
                        _childName = _reader.Value.ToString();
                        break;
                    case JsonToken.EndObject:
                        return;
                        
                }
                if (_childName == null) continue;
                doJsonValue _oneNode = new doJsonValue();
                _oneNode.LoadDataFromeStream(_reader, false);
                this.SetOneValue(_childName, _oneNode);
            }
            throw new Exception("配置文件的格式不正确，z012JsonConfigNode意外结束");
        }

        private Dictionary<string, doJsonValue> dictValues;
        private List<doJsonValue> listValues;
        public bool CaseSensitive;
        private List<string> listKeys;
        public List<doJsonValue> GetAllValues()
        {
            return this.listValues;
        }
        public List<string> getAllKeys()
        {
            return this.listKeys;
        }
        public Dictionary<string, doJsonValue> GetAllKeyValues()
        {
            return this.dictValues;
        }
        public doJsonValue GetOneValue(string _name)
        {
            if (!this.CaseSensitive)
            {
                _name = _name.ToLower();
            }
            if (!this.dictValues.ContainsKey(_name)) return null;
            return this.dictValues[_name];
        }
        public void SetOneValue(string _name, doJsonValue _value)
        {
            string _indexname = _name;
            if (!this.CaseSensitive)
            {
                _indexname = _indexname.ToLower();
            }
            if (!this.dictValues.ContainsKey(_indexname))
            {
                _value.NodeName = _name;
                this.listValues.Add(_value);
                this.listKeys.Add(_name);
                this.dictValues.Add(_indexname, _value);
                return;
            }
            this.dictValues[_indexname] = _value;
        }

        public int GetOneInteger(string _name, int _defaultValue)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue == null) return _defaultValue;
            return _configValue.GetInteger(_defaultValue);
        }

        public double GetOneFloat(string _name, double _defaultValue)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue == null) return _defaultValue;
            return _configValue.GetFloat(_defaultValue);
        }

        public string GetOneText(string _name, string _defaultValue)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue == null) return _defaultValue;
            return _configValue.GetText(_defaultValue);
        }

        public bool GetOneBoolean(string _name, bool _defaultValue)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue == null) return _defaultValue;
            return _configValue.GetBoolean(_defaultValue);
        }

        public doJsonNode GetOneNode(string _name)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue == null) return null;
            return _configValue.GetNode();
        }

        public List<doJsonValue> GetOneArray(string _name)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue == null) return null;
            return _configValue.GetArray();
        }

        public List<doJsonNode> GetOneNodeArray(string _name)
        {
            List<doJsonValue> _listValues = this.GetOneArray(_name);
            if (_listValues == null) return null;
            List<doJsonNode> _resultArray = new List<doJsonNode>();
            foreach (doJsonValue _configValue in _listValues)
            {
                doJsonNode _val = _configValue.GetNode();
                if (_val == null) continue;
                _resultArray.Add(_val);
            }
            return _resultArray;
        }

        public List<string> GetOneTextArray(string _name)
        {
            List<doJsonValue> _listValues = this.GetOneArray(_name);
            if (_listValues == null) return null;
            List<string> _resultArray = new List<string>();
            foreach (doJsonValue _configValue in _listValues)
            {
                string _val = _configValue.GetText(null);
                if (_val == null) continue;
                _resultArray.Add(_val);
            }
            return _resultArray;
        }

        public void SetOneInteger(string _name, int _value)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue != null)
            {
                _configValue.SetInteger(_value);
                return;
            }
            _configValue = new doJsonValue();
            _configValue.SetInteger(_value);
            this.SetOneValue(_name, _configValue);
        }

        public void SetOneFloat(string _name, double _value)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue != null)
            {
                _configValue.SetFloat(_value);
                return;
            }
            _configValue = new doJsonValue();
            _configValue.SetFloat(_value);
            this.SetOneValue(_name, _configValue);
        }

        public void SetOneText(string _name, string _value)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue != null)
            {
                _configValue.SetText(_value);
                return;
            }
            _configValue = new doJsonValue();
            _configValue.SetText(_value);
            this.SetOneValue(_name, _configValue);
        }

        public void SetOneBoolean(string _name, bool _value)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue != null)
            {
                _configValue.SetBoolean(_value);
                return;
            }
            _configValue = new doJsonValue();
            _configValue.SetBoolean(_value);
            this.SetOneValue(_name, _configValue);
        }

        public void SetOneNode(string _name, doJsonNode _node)
        {
            if (_node == this) throw new Exception("设置Json时，发生递归");
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue != null)
            {
                _configValue.SetNode(_node);
                return;
            }
            _configValue = new doJsonValue();
            _configValue.SetNode(_node);
            this.SetOneValue(_name, _configValue);
        }

        public void SetOneArray(string _name, List<doJsonValue> _array)
        {
            doJsonValue _configValue = this.GetOneValue(_name);
            if (_configValue != null)
            {
                _configValue.SetArray(_array);
                return;
            }
            _configValue = new doJsonValue();
            _configValue.SetArray(_array);
            this.SetOneValue(_name, _configValue);
        }

        public void SetOneNodeArray(string _name, List<doJsonNode> _values)
        {
            List<doJsonValue> _newVals = new List<doJsonValue>();
            foreach (doJsonNode _val in _values)
            {
                doJsonValue _newconfig = new doJsonValue();
                _newconfig.SetNode(_val);
                _newVals.Add(_newconfig);
            }
            this.SetOneArray(_name, _newVals);
        }

        public void SetOneTextArray(string _name, List<string> _values)
        {
            List<doJsonValue> _newVals = new List<doJsonValue>();
            foreach (string _val in _values)
            {
                doJsonValue _newconfig = new doJsonValue();
                _newconfig.SetText(_val);
                _newVals.Add(_newconfig);
            }
            this.SetOneArray(_name, _newVals);
        }

        public void ExportToStream(JsonTextWriter _jsonWriter)
        {
            _jsonWriter.WriteStartObject();
            foreach (doJsonValue _configValue in this.listValues)
            {
                _jsonWriter.WritePropertyName(_configValue.NodeName);
                _configValue.ExportToStream(_jsonWriter);
            }
            _jsonWriter.WriteEndObject();
        }
        public string ExportToText()
        {
            doJsonValue _configValue = new doJsonValue();
            _configValue.SetNode(this);
            return _configValue.ExportToText(false);
        }
    }
}